package com.beauty.dressing.module.main.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.beauty.dressing.module.main.MainFragment;
import com.beauty.dressing.module.person.activity.LoginActivity;
import com.framework.common.base.IBaseFragment;
import com.framework.common.utils.ICommonUtil;
import com.beauty.dressing.common.base.BaseFragmentActivity;
import com.beauty.dressing.logic.control.LBController;
import com.beauty.dressing.logic.manager.SharedPreferencesManager;
import com.beauty.dressing.module.community.CommunityFragment;
import com.beauty.dressing.module.market.MarketFragment;
import com.beauty.dressing.module.person.MineFragment;
import com.beauty.dressing.rxandroid.rxbus.RxAction;
import com.beauty.dressing.rxandroid.rxbus.RxBus;
import com.beauty.dressing.rxandroid.rxbus.RxBusObserver;
import com.beauty.dressing.rxandroid.rxbus.RxConstants;
import com.beauty.dressing.R;
import java.util.Arrays;
import java.util.List;
import io.reactivex.disposables.Disposable;

public class MainActivity extends BaseFragmentActivity implements RadioGroup.OnCheckedChangeListener, Handler.Callback {
    public static final int TAG_HOME = 0;
    public static final int TAG_COMMUNITY = 1;
    public static final int TAG_MARKET = 2;
    public static final int TAG_MY = 3;

    private RadioGroup mTabGroup;
    private RadioButton mHomeBtn, mMarketBtn, mCommunityBtn, mMyBtn;
    private Handler mHandler;
    private long lastClickTabId;
    private static final long SCAN_PERIOD = 5000;
    public static final String EXTRA_DEVICE_ADDRESS = "extra_device_address";
    public static final String EXTRA_DEVICE_NAME = "extra_device_name";
    private Disposable mDisposable;
    private SharedPreferencesManager mPreferencesManager;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_main);
        mHandler = new Handler(this);
    }

    @Override
    public void findView() {
        mTabGroup = (RadioGroup) findViewById(R.id.tab_layout);
        mTabGroup.setOnCheckedChangeListener(this);
        mHomeBtn = (RadioButton) findViewById(R.id.home_tab);
        mCommunityBtn = (RadioButton) findViewById(R.id.community_tab);
        mMyBtn = (RadioButton) findViewById(R.id.my_tab);
        mMarketBtn = (RadioButton) findViewById(R.id.market_tab);

        mHomeBtn.setOnClickListener(this);
        mCommunityBtn.setOnClickListener(this);
        mMyBtn.setOnClickListener(this);
        mMarketBtn.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mPreferencesManager = new SharedPreferencesManager();
        fragments = new IBaseFragment[4];
        fragments[TAG_HOME] = new MainFragment();
        fragments[TAG_COMMUNITY] = new CommunityFragment();
        fragments[TAG_MARKET] = new MarketFragment();
        fragments[TAG_MY] = new MineFragment();
        mHomeBtn.setChecked(true);
        lastClickTabId = R.id.home_tab;
        boolean hasLogin = hasLogin();
        registRxBus();
    }

    private Handler mHander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    /**
     * 通过RxBus注册监听事件
     */
    private void registRxBus() {
        List<Integer> list = Arrays.asList(RxConstants.ACTION_QRCODE,RxConstants.ACTION_REMOVE,RxConstants.ACTION_LIMIT);
        RxBus.getDefault().toObservableWithCodes(list).subscribeWith(new RxBusObserver<RxAction>() {
            @Override
            public void onRxSubscribe(Disposable d) {
                mDisposable = d;
            }

            @Override
            public void onRxNext(RxAction value) {
                switch (value.code) {
                    case RxConstants.ACTION_QRCODE://
                        break;
                    case RxConstants.ACTION_REMOVE:
                        break;
                    case RxConstants.ACTION_LIMIT:
                        break;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        controller.clear();
        if (mDisposable != null) {
            mDisposable.dispose();
        }
    }

    @Override
    public boolean handleMessage(Message message) {
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_tab:
                if (fragments[TAG_HOME].isAdded()) {
                    if (!ICommonUtil.isFastDoubleClick(200L)) { //防止以双击方式切换tab,同时执行下拉刷新
                        tabAnimator(mHomeBtn);
                    }
                }
                break;
            case R.id.community_tab:
                if (fragments[TAG_COMMUNITY].isAdded()) {
                    if (!ICommonUtil.isFastDoubleClick(200L)) {
                        tabAnimator(mCommunityBtn);
                    }
                    if (lastClickTabId != R.id.community_tab){
                        ((CommunityFragment)fragments[TAG_COMMUNITY]).tabReFocus();
                    }
                }
                break;
            case R.id.market_tab:
                if (fragments[TAG_MARKET].isAdded()) {
                    if (!ICommonUtil.isFastDoubleClick(200L)) {
                        tabAnimator(mMarketBtn);
                    }
                }
                break;
            case R.id.my_tab:
                if (fragments[TAG_MY].isAdded()) {
                    if (!ICommonUtil.isFastDoubleClick(200L)) {
                        tabAnimator(mMyBtn);
                    }
                }
                LoginActivity.startActivity(context);
                break;
        }
        lastClickTabId = v.getId();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
        switch (checkedId) {
            // 首页
            case R.id.home_tab:
                switchFragment(R.id.content_frame, TAG_HOME);
                break;
            // 商城
            case R.id.market_tab:
                switchFragment(R.id.content_frame, TAG_MARKET);
                break;
            // 社区
            case R.id.community_tab:
                switchFragment(R.id.content_frame, TAG_COMMUNITY);
                break;
            // 我的
            case R.id.my_tab:
                switchFragment(R.id.content_frame, TAG_MY);
                break;
            default:
                break;
        }

    }

    public boolean isCheckedHome() {
        if (null != mTabGroup && R.id.home_tab == mTabGroup.getCheckedRadioButtonId()) {
            return true;
        }
        return false;
    }

    public boolean isCheckedMarket() {
        if (null != mTabGroup && R.id.market_tab == mTabGroup.getCheckedRadioButtonId()) {
            return true;
        }
        return false;
    }

    public boolean isCheckedCommunity() {
        if (null != mTabGroup && R.id.community_tab == mTabGroup.getCheckedRadioButtonId()) {
            return true;
        }
        return false;
    }

    public boolean isCheckedMine() {
        if (null != mTabGroup && R.id.my_tab == mTabGroup.getCheckedRadioButtonId()) {
            return true;
        }
        return false;
    }


    private void tabAnimator(final View view) {
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "scaleX", 1.15f);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "scaleY", 1.15f);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, "scaleX", 1.0f);
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(view, "scaleY", 1.0f);

        ObjectAnimator animator5 = ObjectAnimator.ofFloat(view, "scaleX", 1.10f);
        ObjectAnimator animator6 = ObjectAnimator.ofFloat(view, "scaleY", 1.10f);
        ObjectAnimator animator7 = ObjectAnimator.ofFloat(view, "scaleX", 1.0f);
        ObjectAnimator animator8 = ObjectAnimator.ofFloat(view, "scaleY", 1.0f);
        animatorSet.play(animator1).with(animator2).before(animator3);
        animatorSet.play(animator3).with(animator4).before(animator5);
        animatorSet.play(animator5).with(animator6).before(animator7);
        animatorSet.play(animator7).with(animator8);
        animatorSet.setDuration(100);
        animatorSet.start();
    }

    /**
     * 设置再按一次退出程序
     */
    private boolean isExit = false;

    @Override
    public void onBackPressed() {
        if (!isExit) {
            isExit = true;
            showToast("再按一次退出程序");
            LBController.MainHandler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
            return;
        } else {
            super.onBackPressed();
        }
    }
}
